<?php
if (!defined("_INDEX")) { header("Location: /"); die; }

$joined_id = $route->getByNum(5);

$joined = $db->getRow("select `J`.*,`R`.`user_id` from `joined` as `J`,`reports` as `R` where `J`.`joined_id` = '".$joined_id."' and `J`.`report_id` = `R`.`report_id` limit 1");

if ($joined && $joined['user_id'] == $user->getUserId())
{
	$db->sendQuery("delete from `joined` where `joined_id` = '$joined_id'"); // удаляем запись
	$reports = $db->getCell("select count(`joined_id`) from `joined` where `report_id` = '".$joined['report_id']."'");
	if ($reports == 0) // если больше не осталось записей из отчета report_id, удаляем отчет
		$db->sendQuery("delete from `reports` where `report_id` = '".$joined['report_id']."'");
}

goToPage("/coordinator/reports/look");
?>